package com.zxk.crawler.service;

import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class ParseJDData {
    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

//    public List<Phone> getJDdata(String keyword){
//        try {
//            URL url = new URL("https://search.jd.com/Search?keyword="+keyword);
//            Document parse = Jsoup.parse(url, 3000);
//            Elements li = parse.getElementById("J_goodsList").getElementsByClass("gl-item");
//            List<Phone> phoneList = new ArrayList<>();
//            BulkRequest request = new BulkRequest();
//            request.timeout("2m");
//            for (Element el : li) {
//                String img = el.getElementsByTag("img").get(0).attr("data-lazy-img");
//                String title  = el.getElementsByClass("p-name").eq(0).text();
//                String price = el.getElementsByClass("p-price").eq(0).text();
//                Phone phone = new Phone();
//                phone.setImg(img);
//                phone.setPrice(price);
//                phone.setTitle(title);
//                phoneList.add(phone);
//            }
//            for (int i = 0; i < phoneList.size(); i++) {
//                request.add(new IndexRequest("phone").source(JSON.toJSONString(phoneList.get(i)), XContentType.JSON));
//            }
//            BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
//            return phoneList;
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    public List<Map<String,Object>> searchData(String keyword, Integer pageSize, Integer pageNo){
//        List<Map<String,Object>> list = new ArrayList<>();
//        try {
//            SearchRequest request = new SearchRequest("phone");
//            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//            MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("title", keyword);
//            sourceBuilder.from(pageNo);
//            sourceBuilder.size(pageSize);
//
//            //高亮
//            HighlightBuilder highlightBuilder = new HighlightBuilder();
//            highlightBuilder.field("title");
//            highlightBuilder.preTags("<span style='color:blue'>");
//            highlightBuilder.postTags("</span>");
//            sourceBuilder.highlighter(highlightBuilder);
//
//            sourceBuilder.query(queryBuilder);
//            sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
//            request.source(sourceBuilder);
//            SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
//            for (SearchHit documentFields : searchResponse.getHits().getHits()) {
//                HighlightField title = documentFields.getHighlightFields().get("title");
//                Map<String, Object> sourceAsMap = documentFields.getSourceAsMap();
//                if(null != title) {
//                    StringBuilder highField = new StringBuilder();
//                    Text[] fragments = title.fragments();
//                    for (Text fragment : fragments) {
//                        highField.append(fragment);
//                    }
//                    sourceAsMap.put("title",highField.toString());
//                }
//                list.add(sourceAsMap);
//            }
//            return list;
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return list;
//    }
}
